package metaphor;

import ingest.europarl.CorporaDB;
import ingest.europarl.SentenceGroup;
import ingest.wiktionary.types.WiktionaryEntry;

import java.util.Iterator;
import java.util.List;

public class MetaphorAnnotator {

	private Iterator<SentenceGroup> sentenceIterator;
	private List<WiktionaryEntry> wiktionaryEntries;
	private CorporaDB corpDB;

	public boolean annotateSentence(String sentence, String metaphor) {
		if (sentence.toLowerCase().contains(metaphor.toLowerCase())) {
			System.out.println(sentence + "contains " + metaphor);
			return true;
		} else
			return false;
	}

	public MetaphorAnnotator(Iterator<SentenceGroup> sentenceIterator,
			List<WiktionaryEntry> wiktionaryEntries) {
		this.sentenceIterator = sentenceIterator;
		this.wiktionaryEntries = wiktionaryEntries;
		this.corpDB = CorporaDB.getInstance();

	}

	public void annotateSentencesInLanguage(String language) {
		// Each sentence looks at every entry, to see if it has one of the
		// idioms it needs.
		while (sentenceIterator.hasNext()) {
			SentenceGroup sentence = sentenceIterator.next();
			String englishSentence = sentence.getSentence(language);
			List<WiktionaryEntry> newEntryList = sentence.getEntries();
			for (WiktionaryEntry entry : wiktionaryEntries) {
				boolean sentenceContainsEntry = false;
				for (String idiom : entry.getAlternativeForms()) {
					// if it has the idiom, add it to the list of references
					sentenceContainsEntry = annotateSentence(englishSentence,
							idiom);

				}
				if (sentenceContainsEntry) {
					System.out.println("--" + entry.getIdiom() + "--");

					newEntryList.add(entry);
				}
			}
			if (!newEntryList.isEmpty())
				corpDB.update(sentence, "entries", newEntryList);

		}
	}
}
